<template>
    <!-- 左侧 -->
    <div class="home-category" @mouseleave="categoryId = null">
        <ul class="menu">
            <li
                v-for="item in menuList"
                :key="item.id"
                @mouseenter="mouseEnterLi(item)"
            >
                <RouterLink :to="`/category/${item.id}`">{{
                    item.name
                }}</RouterLink>

                <template v-if="item.sub">
                    <router-link
                        v-for="s in item.sub"
                        :key="s.children_id"
                        :to="`/category/sub/${s.children_id}`"
                        >{{ s.children_name }}</router-link
                    >
                </template>
                <template v-else>
                    <skeleton
                        width="60px"
                        height="18px"
                        style="margin-left: 5px"
                    ></skeleton>
                    <skeleton
                        width="60px"
                        height="18px"
                        style="margin-left: 5px"
                    ></skeleton>
                </template>
            </li>
        </ul>

        <!-- 弹层 -->
        <div class="layer">
            <h4>
                {{ categoryId == 'brand' ? '品牌推荐' : '分类推荐' }}
                <small>根据您的购买或浏览记录推荐</small>
            </h4>
            <ul
                v-if="
                    currentCategory &&
                    currentCategory.goods &&
                    currentCategory.goods.length
                "
            >
                <li v-for="goods in currentCategory.goods" :key="goods.id">
                    <RouterLink :to="`/goods/${goods.goods_id}`">
                        <img
                            :src="goods.goods_picture"
                            :alt="goods.goods_name"
                        />
                        <div class="info">
                            <p class="name ellipsis-2">
                                {{ goods.goods_name }}
                            </p>
                            <p class="desc ellipsis">{{ goods.goods_desc }}</p>
                            <p class="price">
                                <i>¥</i>{{ goods.goods_price }}元
                            </p>
                        </div>
                    </RouterLink>
                </li>
            </ul>
            <!-- 品牌 -->
            <ul
                v-if="
                    currentCategory &&
                    currentCategory.brands &&
                    currentCategory.brands.length
                "
            >
                <li
                    class="brand"
                    v-for="brand in currentCategory.brands"
                    :key="brand.id"
                >
                    <RouterLink to="/">
                        <img :src="brand.picture" :alt="brand.name" />
                        <div class="info">
                            <p class="name ellipsis">{{ brand.name }}</p>
                            <p class="desc ellipsis-2">{{ brand.desc }}</p>
                        </div>
                    </RouterLink>
                </li>
            </ul>
        </div>
    </div>
</template>

<script setup>
import { computed, reactive, ref } from 'vue'
import { categoryStore } from '@/stores/category'
import { homeStore } from '@/stores/home'
const useCategoryStore = categoryStore()
const useHomeStore = homeStore()
const brand = computed(() => useHomeStore.brand)
let list = reactive([])
list = computed(() => Array.from(useCategoryStore.subList))
// 定义一个品牌分类
const categoryId = ref(0)
const menuList = computed(() => {
    list.value.push(brand.value)
    return list.value
})

const currentCategory = computed(() => {
    return menuList.value.find(item => {
        return categoryId.value == item.id
    })
})
// 鼠标进入li中让弹层显示函数

const mouseEnterLi = item => {
    categoryId.value = item.id
    if (categoryId.value == 'brand') return
}
</script>

<style lang="scss" scoped>
.home-category {
    width: 250px;
    height: 510px;
    background: rgba(0, 0, 0, 0.8);
    border-radius: 20px;
    padding-top: 10px;
    position: relative;
    z-index: 99;
    transform: translateX(-50px);
    .menu {
        z-index: 10;
        li {
            @include hoverShadow;
            padding-left: 40px;
            height: 50px;
            line-height: 50px;
            transition: all 0.2s ease;
            border-radius: 20px;
            &:hover {
                background: #da9233;
            }
            a {
                margin-right: 4px;
                color: #fff;
                &:first-child {
                    font-size: 16px;
                }
            }
        }
    }

    .layer {
        border-radius: 20px;
        width: 990px;
        height: 500px;
        background: rgba(255, 255, 255, 0.8);
        position: absolute;
        left: 245px;
        top: 0;
        padding: 0 15px;
        visibility: hidden;
        z-index: 0;
        opacity: 0;
        transition: all 0.2s ease;
        h4 {
            font-size: 20px;
            font-weight: normal;
            line-height: 80px;
            position: relative;
            small {
                font-size: 16px;
                color: #666;
            }
        }
        ul {
            display: flex;
            flex-wrap: wrap;
            li {
                @include hoverShadow;
                width: 310px;
                height: 120px;
                margin-right: 15px;
                margin-bottom: 15px;
                border: 1px solid #eee;
                border-radius: 4px;
                background: #fff;
                &:nth-child(3n) {
                    margin-right: 0;
                }
                a {
                    display: flex;
                    width: 100%;
                    height: 100%;
                    align-items: center;
                    padding: 10px;
                    transition: all 0.2s ease;
                    &:hover {
                        background: #f2c3809c;
                    }
                    img {
                        width: 95px;
                        height: 95px;
                    }
                    .info {
                        padding-left: 10px;
                        line-height: 24px;
                        width: 190px;
                        .name {
                            font-size: 16px;
                            color: #666;
                        }
                        .desc {
                            color: #999;
                        }
                        .price {
                            font-size: 22px;
                            color: red;
                            i {
                                font-size: 16px;
                            }
                        }
                    }
                }
            }
        }
    }
    &:hover {
        .layer {
            left: 250px;
            opacity: 1;
            visibility: visible;
            z-index: 10;
        }
    }
}
li.brand {
    height: 180px;
    a {
        align-items: flex-start;
        img {
            width: 120px;
            height: 160px;
        }
        .info {
            p {
                margin-top: 8px;
            }
            .place {
                color: #999;
            }
        }
    }
}
</style>
